package com.hexa.wonderland.sdk;

import android.util.Log;

import com.hexa.wonderland.sdk.ad.HeVideoRewardCallback;
import com.hexa.wonderland.sdk.ad.HeVideoIAP;
import com.hexa.wonderland.sdk.bridge.HeCocosBridge;
import com.hexa.wonderland.sdk.ad.HeVideoManger;
import com.hexa.wonderland.sdk.ad.HeMaxReward;

import org.json.JSONObject;

public class HeAdSdk {

    public static void showBiddingAd(String ad_source) {
        showAd(new HeVideoRewardCallback() {
            @Override
            public void onAdOver() {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("ad_source",ad_source);
                    HeCocosBridge.getInstance().XSSdkCallback("ad_over_hex", jsonObject.toString());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onAdError() {
                HeCocosBridge.getInstance().XSSdkCallback("ad_error_hex", "{}");
            }
        });
    }

    private static void showAd(HeVideoRewardCallback callback) {
        HeVideoIAP service = null;
        boolean isMaxReady =  HeMaxReward.getInstance().isReady();
        if (isMaxReady){
            Log.e("HexaW","show max reward");
            service= HeVideoManger.getReward1Instance();
        }

        if (service == null) {
            callback.onAdError();
            return;
        }

        try {
            service.showVideo(HeCocosBridge.mainGameActivity.get(), new HeVideoRewardCallback() {
                @Override
                public void onAdOver() {
                    if (callback != null) {
                        callback.onAdOver();
                    }
                }

                @Override
                public void onAdError() {
                    if (callback != null) {
                        callback.onAdError();
                    }
                }
            });
        } catch (Exception e) {
            try {
                callback.onAdError();
            } catch (Exception e1) {
            }
        }
    }

}
